
function GMTToStr(timestamp) {
    var time = new Date(timestamp);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}
function add0(m) { return m < 10 ? '0' + m : m }
//获取时间戳对应的时间，格式YYYY-MM-DD
function getTsFormatDate(timeStamp) {
    var date = new Date(timeStamp);
    //console.log(date); 结果为：Tue Apr 02 2019 07:49:23 GMT+0800 (中国标准时间)
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
}
//获取现在对应的时间，格式YYYY-MM-DD
function getNowFormatDate() {
    var timeStamp = new Date().getTime();
    return getTsFormatDate(timeStamp);
}

//获取指定日期对应的时间，格式YYYY-MM-DD，比如一天后，参数则为-1，二天后，参数则为2
function getSpecialFormatDate(dayOffset) {
    var timeStamp = new Date().getTime() + dayOffset * 1000 * 24 * 60 * 60;
    return getTsFormatDate(timeStamp);
}
//分钟转时间
function ChangeHourMinutestr(str) {
    if (str !== "0" && str !== "" && str !== null) {
        return ((Math.floor(str / 60)).toString().length < 2 ? "0" + (Math.floor(str / 60)).toString() :
            (Math.floor(str / 60)).toString()) + ":" + ((str % 60).toString().length < 2 ? "0" + (str % 60).toString() : (str % 60).toString());
    }
    else {
        return "";
    }
}
// 时间戳转多少时间后
function dateDiff(timestamp) {
    // 补全为13位
    var arrTimestamp = (timestamp + '').split('');
    for (var start = 0; start < 13; start++) {
        if (!arrTimestamp[start]) {
            arrTimestamp[start] = '0';
        }
    }
    timestamp = arrTimestamp.join('') * 1;
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var now = new Date().getTime();
    var diffValue = timestamp - now;
    // 如果本地时间反而小于变量时间
    if (diffValue < 0) {
        return '不久后';
    }
    // 计算差异时间的量级
    var monthC = diffValue / month;
    var weekC = diffValue / (7 * day);
    var dayC = diffValue / day;
    var hourC = diffValue / hour;
    var minC = diffValue / minute;
    // 数值补0方法
    var zero = function (value) {
        if (value < 10) {
            return '0' + value;
        }
        return value;
    };

    // 使用
    if (monthC > 4) {
        // 超过1年，直接显示年月日
        return (function () {
            var date = new Date(timestamp);
            return date.getFullYear() + '年' + zero(date.getMonth() + 1) + '月' + zero(date.getDate()) + '日';
        })();
    } else if (monthC >= 1) {
        return parseInt(monthC) + "月后";
    } else if (weekC >= 1) {
        return parseInt(weekC) + "周后";
    } else if (dayC >= 1) {
        return parseInt(dayC) + "天后";
    } else if (hourC >= 1) {
        return parseInt(hourC) + "小时后";
    } else if (minC >= 1) {
        return parseInt(minC) + "分钟后";
    }
    return '刚刚';
};
/*
*
* 存储cokie
* */
function setCookie(name, value) {
    var Days = 90;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    if (name === "token" || name === "referer_url" || name === "userNo" || name === "app") {
        removeCookie(name);
        document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/";
    } else {
        document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
    }
}

function removeCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 30 * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape("") + ";expires=" + exp.toGMTString();
    try {
        if (name === "token" || name === "referer_url" || name === "userNo" || name == "app") {
            document.cookie = name + "=" + escape("") + ";expires=" + exp.toGMTString() + ";path=/";
        }
    } catch (e) { }
}

function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg)) return unescape(arr[2]);
    else return null;
}
function back() {
    window.history.back();
}

function getArrDifference(arr1, arr2) {
    return arr1.concat(arr2).filter(function (v, i, arr) {
        return arr.indexOf(v) === arr.lastIndexOf(v);
    });
}
export {
    GMTToStr,
    getTsFormatDate,
    getNowFormatDate,
    getSpecialFormatDate,
    ChangeHourMinutestr,
    setCookie,
    removeCookie,
    getCookie,
    back,
    getArrDifference,
    dateDiff
}
